var express = require('express');
var router = express.Router();
var db = require('../config/db');

router.post('/Get_libinfo', function(req, res, next) {
	if(req.session.user === "nobody")
		res.render('index', {
			title: 'Login'
		});
	else {
		var lnumber = "Lib-00" + req.body.strs;
		try {
			var sql = "select * from lib where Lib_name = '" + lnumber + "'";
			db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
				} else {
					if(rows.length !== 0) {
						res.send({
							jsondata: rows
						});
					} else
						res.send({
							jsondata: {}
						});
				}
			});
		} catch(e) {
			console.log(e.toString());
		}

	}
});
router.post('/Fresh_libmanage', function(req, res, next) {
	var lnumber = "Lib-00" + req.body.strs;
	try {
		var sql = "select * from member where stu_type = '" + lnumber + "'";
		db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
				} else {
					if(rows.length !== 0) {
						memberarray = rows;
						sql = "select * from action where ActionLib = '" + lnumber + "'";
						db.query(sql, function(err, rows) {
								if(err) {
									console.log(err.toString());
								} else {
									if(rows.length !== 0) {
										actionarray = rows;
										sql = "select * from usinginfo where lib = '" + lnumber + "'";
										db.query(sql, function(err, rows) {
												if(err) {
													console.log(err.toString());
												} else {
													if(rows.length !== 0) {
														usinginfoarray = rows;
														sql = "select * from pc where pc_libname = '" + lnumber + "'";
														db.query(sql, function(err, rows) {
																if(err) {
																	console.log(err.toString());
																} else {
																	if(rows.length !== 0) {
																		Pcarray = rows;
																		var obj = {
																			"memberarray": memberarray,
																			"Pcarray": Pcarray,
																			"actionarray": actionarray,
																			"usinginfoarray": usinginfoarray,
																		}
																		console.log(obj);
																		res.send({
																			res: obj
																		})
																	}
																}
															
														});
												}

											}
										});
								}

							}
						});
				}

			}
		});
}
	catch(e) {
		console.log(e.toString());
	}
});

router.post('/Fresh_pcmanage', function(req, res, next) {
	if(req.session.user === "nobody")
		res.render('index', {
			title: 'Login'
		});
	else {
		try {
			var sql = "select * from pc ";
			db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
				} else {
					if(rows.length !== 0) {
						res.send({
							jsondata: rows
						});
					} else
						res.send({
							jsondata: {}
						});
				}
			});
		} catch(e) {
			console.log(e.toString());
		}

	}
});

router.post('/Add_pcmanage', function(req, res, next) {
	  var id = req.body.id;
	  var os = req.body.os
	  var ip = req.body.ip;
	  var location = req.body.location;
	  var lib = req.body.lib;   
		try {
			var sql = "insert into pc values('"+id+"','"+location+"','"+ip+"','"+os+"','"+lib+"','---');";
			db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
				} else {
					res.send({
					         "id":id,
					         "os":os,
					         "ip":ip,
					         "location":location,
					         "lib":lib,
					           
						});
				}
			});
		} catch(e) {
			console.log( "sql exception in add_pcmanage"+e.toString());
		}

});
router.post('/Delete_pcmanage', function(req, res, next) {
	  var id = req.body.id;
		try {
			var sql = "delete from pc where id ='"+id+"'";
			db.query(sql, function(err, rows) {
				if(err) {
					   res.send("error");
				} else {
					  res.send(id);
				}
			});
		} catch(e) {
			console.log( "sql exception in Delete_pcmanage"+e.toString());
		}

});
router.post('/Modify_pcmanage', function(req, res, next) {
	  var id = req.body.ID;
	  var os = req.body.OS;
	  var ip = req.body.IP;
	  var la = req.body.LOCATION;
	  var lib = req.body.LIB;
		try {
			var sql = "update pc set pc_os ='"+os+"',pc_ip ='"+ip+"',pc_location ='"+la+"',pc_libname ='"+lib+"'where id ='" +id +"'";
			db.query(sql, function(err, rows) {
				if(err) {
					   console.log(err);
					    res.render("error")
				} else {
					  res.redirect('/Lib_pcmanage.html')
				}
			});
		} catch(e) {
			console.log( "sql exception in Delete_pcmanage"+e.toString());
		}

});
router.post('/Fresh_stumanage', function(req, res, next) {
if(req.session.user === "nobody")
		res.render('index', {
			title: 'Login'
		});
	else {
		try {
			var sql = "select * from member ";
			db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
				} else {
					if(rows.length !== 0) {
						res.send({
							jsondata: rows
						});
					} else
						res.send({
							jsondata: {}
						});
				}
			});
		} catch(e) {
			console.log(e.toString());
		}

	}

});
router.post('/class_stumanage', function(req, res, next) {

	  var newclass = req.body.newclass;
	  var id = req.body.id;
		try {
			var sql = "update member set stu_class ='"+newclass+"'where id ='" +id +"'";
			db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
					res.send({mes:"wrong"})
				} else {
					res.send({mes:"ok"})
				}
			});
		} catch(e) {
			console.log(e.toString());
		}

});
router.post('/phone_stumanage', function(req, res, next) {

	  var newphone = req.body.newphone;
	  var id = req.body.id;
		try {
			var sql = "update member set stu_phone ='"+newphone+"'where id ='" +id +"'";
			db.query(sql, function(err, rows) {
				if(err) {
					console.log(err.toString());
					res.send({mes:"wrong"})
				} else {
					res.send({mes:"ok"})
				}
			});
		} catch(e) {
			console.log(e.toString());
		}

});
router.post('/Delete_stumanage', function(req, res, next) {
	  var id = req.body.id;
		try {
			var sql = "delete from member where id ='"+id+"'";
			db.query(sql, function(err, rows) {
				if(err) {
					   res.send("error");
				} else {
					  res.send(id);
				}
			});
		} catch(e) {
			console.log( "sql exception in Delete_pcmanage"+e.toString());
		}

});


router.post('/Addstumanage', function(req, res, next) {
		var stuid = req.body.stuid;
		var name = req.body.name;
		var classs = req.body.classs;
		var sex = req.body.sex;
		var  phone= req.body.phone;
		var lib = req.body.lib;
		try {
		var sql = "insert into member values('"+stuid+"','"+name+"','"+sex+"','"+classs+"','"+phone+"','"+lib+"','"+stuid+"');";
			db.query(sql, function(err, rows) {
				if(err) {
					   res.send("error");
				} else {
					res.send([stuid,name,classs,sex,phone,lib]);
				}
			});
		} catch(e) {
			console.log( "sql exception in Delete_pcmanage"+e.toString());
		}

});

router.post('/add_writing', function(req, res, next) {
		var begin = req.body.begin;
		var over = req.body.over;
		var name = req.body.name;
		var pcid = req.body.pcid;
		var  lib= req.body.lib;
		console.log("wring log in file")
		try {
		var sql = "insert into usinginfo values('"+begin+"','"+over+"','"+name+"','"+pcid+"','"+lib+"');";
			db.query(sql, function(err, rows) {
				if(err) {
					   res.send("error");
				} else {
					res.send("ok");
				}
			});
		} catch(e) {
			console.log( "sql exception in Delete_pcmanage"+e.toString());
		}

});


module.exports = router;